package com.paipaideli.ui.home;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterFragment;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.PullRefreshView;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.ui.home.adapter.HomePageAdapter;
import com.paipaideli.ui.home.bean.HomeBean;
import com.paipaideli.ui.news.MyNewsActivity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class HomeFragment extends BasePresenterFragment {

    Unbinder unbinder;
    @BindView(R.id.home_recyclerview)
    PullRefreshView home_recyclerview;
    @BindView(R.id.rel_home_new)
    RelativeLayout rel_home_new;

    private HomePageAdapter homePageAdapter;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initView() {
        //消息中心
        RxView.clicks(rel_home_new).subscribe(o -> {
            Intent intent = new Intent(getActivity(), MyNewsActivity.class);
            startActivity(intent);
        }, throwable -> {
        });

        home_recyclerview.setEnableRefresh(false);
        home_recyclerview.setEnableLoadmore(false);
        //设置是否在全部加载结束之后Footer跟随内容
        home_recyclerview.setOnRefreshListener(refreshlayout -> {
            homePageAdapter.clear();
            initDate();
            home_recyclerview.finishRefresh();
        });

//        //2、通过Resources获取屏幕的宽高
//        DisplayMetrics dm = getResources().getDisplayMetrics();
//        int width = dm.widthPixels;
//        ViewGroup.LayoutParams para;
//        para = home_recyclerview.getLayoutParams();
//        para.width = width;
//        home_recyclerview.setLayoutParams(para);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getCurrentActivity());
        home_recyclerview.setLayoutManager(linearLayoutManager);
        homePageAdapter = new HomePageAdapter(getCurrentActivity());
        home_recyclerview.setAdapter(homePageAdapter);

    }

    @Override
    public void onStart() {
        super.onStart();

        home_recyclerview.showLoading();
        initDate();
    }

    public void initDate() {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("curPage", "1");
            requestData.put("pageSize", "50");
        } catch (Exception e) {
            e.getMessage();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
        Api.getInstanceGson().home(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(request -> {
                    home_recyclerview.finishLoading();
                    homePageAdapter.clear();
                    if (request.code.equals("200")) {
                        homePageAdapter.setDate(request.data);
                        homePageAdapter.notifyDataSetChanged();
                    } else {
                        home_recyclerview.showEmpty(request.msg);
                    }
                }, throwable -> {
                    Log.i("TAG",throwable.getMessage());
                    home_recyclerview.showError(throwable);
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if (!hidden) {
//            System.gc();
//            initDate();
//        }
//    }
}
